package com.example.aitoedu.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.example.aitoedu.dto.SubmitDTO;
import com.example.aitoedu.entity.Stestbank;
import com.example.aitoedu.mapper.ScMapper;
import com.example.aitoedu.mapper.StestbankMapper;
import com.example.aitoedu.service.StestbankService;
import com.example.aitoedu.utils.ReturnHandle;
import com.example.aitoedu.vo.ChapterVO;
import com.example.aitoedu.vo.MyQuestionVO;
import com.example.aitoedu.vo.QuestionVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @author 曹正豪
* @description 针对表【STestBank(学生作业)】的数据库操作Service实现
* @createDate 2024-10-15 11:12:09
*/
@Service
public class StestbankServiceImpl extends ServiceImpl<StestbankMapper, Stestbank>
    implements StestbankService{

    @Autowired
    private StestbankMapper stestbankMapper;

    @Autowired
    private ScMapper scMapper;
    @Override
    public int score (Long chapterId) {
        Long sid = Long.valueOf(StpUtil.getLoginId().toString());
        return stestbankMapper.score(sid,chapterId);
    }

    @Override
    public List<QuestionVO> getAllQuestion(Long chapterId) {
        Long sid = Long.valueOf(StpUtil.getLoginId().toString());
        List<QuestionVO> questionVOList = stestbankMapper.getAllQuestion(sid,chapterId);
        if(questionVOList != null)
            for(int i = 0; i < questionVOList.size(); i++) questionVOList.get(i).setNumberId(Long.valueOf(i+1));
        return ReturnHandle.questionReturn(questionVOList);
    }

    @Override
    public void submit(List<SubmitDTO> submitDTOList) {
        Long sid = Long.valueOf(StpUtil.getLoginId().toString());
        List<Stestbank> list = new ArrayList<>();
        for(SubmitDTO s:submitDTOList){
            QuestionVO questionVO = ReturnHandle.questionReturnOnly(stestbankMapper.submitSelect(s.getQuestionId()));
            System.out.println(questionVO+"---------------------------");
            Stestbank stestbank = new Stestbank();
            stestbank.setId(s.getQuestionId());
            stestbank.setSid(sid);
            switch (questionVO.getType()){
                case 1:
                    if(s.getAnswers() != null){
                        stestbank.setAnswer(String.join("`",s.getAnswers()));
                        if(questionVO.getAnswers().get(0).equals(s.getAnswers().get(0))){
                            stestbank.setStuScore(questionVO.getScore());
                            stestbank.setStuStatus(1);
                        }else {
                            stestbank.setStuScore(0);
                            stestbank.setStuStatus(0);
                        }
                    }
                    break;
                case 2:
                    if(s.getAnswers() != null){
                        stestbank.setAnswer(String.join("`",s.getAnswers()));
                        Set<String> right = questionVO.getAnswers().stream().collect(Collectors.toSet());
                        Set<String> stuAnswer = s.getAnswers().stream().collect(Collectors.toSet());
                        if(stuAnswer.containsAll(right) && right.size() == stuAnswer.size()) {
                            stestbank.setStuScore(questionVO.getScore());
                            stestbank.setStuStatus(1);
                        }else if(right.containsAll(stuAnswer) && right.size() != stuAnswer.size()){
                            stestbank.setStuScore(questionVO.getShortChoose());
                            stestbank.setStuStatus(3);
                        }else{
                            stestbank.setStuScore(0);
                            stestbank.setStuStatus(0);
                        }
                    }
                    break;
                case 3:
                    if(s.getAjudge() != null){
                        stestbank.setAjudge(s.getAjudge());
                        if(s.getAjudge() == questionVO.getAjudge()){
                            stestbank.setStuScore(questionVO.getScore());
                            stestbank.setStuStatus(1);
                        }else{
                            stestbank.setStuScore(0);
                            stestbank.setStuStatus(0);
                        }
                    }
                    break;
                case 4:
                    if(s.getAnswers() != null){
                        stestbank.setAnswer(String.join("`",s.getAnswers()));
                        int score = 0;
                        for(int i = 0 ; i<questionVO.getAnswers().size(); i++){
                            if(s.getAnswers().get(i) == null) continue;
                            if(questionVO.getAnswers().get(i).equals(s.getAnswers().get(i)))
                                score+=questionVO.getEveryBlanks().get(i);
                        }
                        stestbank.setStuScore(score);
                        if(score == questionVO.getScore()) stestbank.setStuStatus(1);
                        else if(score == 0) stestbank.setStuStatus(0);
                        else stestbank.setStuStatus(3);
                    }
                    break;
                case 5:
                    if(s.getAnswers() != null)
                        stestbank.setAnswer(s.getAnswers().get(0));
                        stestbank.setStuStatus(0);
                        stestbank.setStuScore(0);
                    break;
            }
            list.add(stestbank);
        }
        stestbankMapper.updateById(list);
    }

    @Override
    public List<MyQuestionVO> myQuestion() {
        Long sid = Long.valueOf(StpUtil.getLoginId().toString());
        List<MyQuestionVO> myQuestionVOList = scMapper.getCidsNameBySid(sid);
        for(MyQuestionVO m:myQuestionVOList){
            List<ChapterVO> chapterVOList = stestbankMapper.myQuestion(sid, m.getCourseId());
            for(ChapterVO c:chapterVOList){
                List<QuestionVO> questionVOList = ReturnHandle.questionReturn(stestbankMapper.getAllQuestion(sid,c.getChapterId()));
                c.setQuestionVOList(questionVOList);
            }
            m.setChapterVOList(chapterVOList);
        }
        return myQuestionVOList;
    }
}





